package com.shaoshaohuo.app.ui.ec;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.constant.ListViewAction;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.PurchaseQuote;
import com.shaoshaohuo.app.entity.PurchaseQuoteEntity;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.adapter.QuoteListAdapter;
import com.shaoshaohuo.app.view.TopbarView;

/**
 * @ClassName QuoteListActivity
 * @author linmu
 * @Decription 
 * @2015-8-30 下午5:42:50
 */
public class QuoteListActivity extends BaseActivity {

	private TopbarView mTopbarView;
	
	private ListView mListView;

	private String orderid;
	private String cursor = "";
	private int size = Integer.MAX_VALUE;
	private String action = ListViewAction.NEW;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_quote_list);
		
		initData();
		initView();
		setUpView();
		requestData();
	}

	private void initData() {
		orderid = getIntent().getStringExtra(ExtraName.id);
	}

	private void requestData() {
		startLoadingDialog();
		RequestService.getInstance().getPurchaseQuoteList(this, orderid, cursor, size+"", action, PurchaseQuoteEntity.class, new RequestListener() {
			
			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if(resultData.isOk()){
					PurchaseQuoteEntity entity = (PurchaseQuoteEntity) resultData;
					setAdapter(entity);
				}else{
					showToast(resultData.getMsg());
				}
			}
			
			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(R.string.please_check_network);
			}
		});
	}

	protected void setAdapter(PurchaseQuoteEntity entity) {
		
//		if(entity.getData().getList().isEmpty()){
//			PurchaseQuote quote = new PurchaseQuote();
//			entity.getData().getList().add(quote);
//			entity.getData().getList().add(quote);
//			entity.getData().getList().add(quote);
//			entity.getData().getList().add(quote);
//			entity.getData().getList().add(quote);
//			entity.getData().getList().add(quote);
//			entity.getData().getList().add(quote);
//			entity.getData().getList().add(quote);
//		}
		
		QuoteListAdapter<PurchaseQuote> adapter = new QuoteListAdapter<PurchaseQuote>(this, entity.getData().getList(), false);
        mListView.setAdapter(adapter);
	}

	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);

		mListView = (ListView) this.findViewById(R.id.listview_data_list);
	}

	private void setUpView() {
		mTopbarView.setCenterText("商家报价");
		mTopbarView.setLeftView(true, true);
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            	PurchaseQuote quote = (PurchaseQuote) parent.getItemAtPosition(position);
                Intent intent = new Intent(QuoteListActivity.this,QuoteDetailActivity.class);
                intent.putExtra(ExtraName.id, quote.getId());
                intent.putExtra(ExtraName.orderid, orderid);
                startActivity(intent);
            }
        });
        
        
	}
	
}
